﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace CodeGenerate
{
    /// <summary>
    /// 转换
    /// </summary>
    public class ConvertUtil
    {
        private static string IsNotNullable = "EnableRequiredFieldValidator='true'";
        private static string ReplaceSql = "ReplaceTextType='Sql' ";
        private static string MaxLength = "MaxLength='{0}'";
        
        protected static string TextBox = @"<aspnet:ExTextBox ID='{0}' runat='server' RequiredFieldType='{1}'    {2}  ValidationGroup='{3}'  {4}  {5}  {6}/>";
        protected static string DropDownList = @"<aspnet:ExDropDownList ID='{0}'  runat='server' {1}   ValidationGroup='{2}'  />";
        protected static string CheckBox = @"<asp:ExCheckBox ID='{0}' runat='server' Text='{1}' ValidationGroup='{2}'/>";
        /// <summary>
        /// 获取控件ID
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public static string GetName(string prefix, Column col)
        {
            prefix = !string.IsNullOrEmpty(prefix) ? prefix + "_" : string.Empty;
            if (col.DataType == DbType.Boolean)
            {
                return string.Format("cb{0}{1}", prefix,Utils.ConvertName(col.Name));
            }
            else if (col.DataType == DbType.Guid)
            {
                return string.Format("box{0}{1}", prefix, Utils.ConvertName(col.Name));
            }
            else
            {
                return string.Format("txt{0}{1}", prefix, Utils.ConvertName(col.Name));
            }
        }

        /// <summary>
        /// 编辑 asp.net标签
        /// </summary>
        /// <param name="col"></param>
        /// <returns></returns>
        public static string ParseToEditForm(Column col)
        {
            return ParseToForm(col, string.Empty,true,"EditForm",false);
        }

        /// <summary>
        /// 查询 asp.net 标签
        /// </summary>
        /// <param name="col"></param>
        /// <returns></returns>
        public static string ParseToSearchForm(Column col)
        {
            return ParseToForm(col, "Search",false,"QueryForm",true);
        }

        /// <summary>
        /// 获取 designer.cs 声明
        /// </summary>
        /// <param name="col"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static string ParseGetControlDeclare(Column col)
        {
            if (col.DataType == DbType.Boolean)
            {
                return string.Format("ExCheckBox");
            }
            else if (col.DataType == DbType.Guid)
            {
                return string.Format("ExDropDownList");
            }
            return string.Format("ExTextBox");
        }

       
        /// <summary>
        /// 生成 asp.net 标签
        /// </summary>
        /// <param name="col"></param>
        /// <param name="prefix"></param>
        /// <param name="mustValidate">是否要验证</param>
        /// <returns></returns>
        public static string ParseToForm(Column col,string prefix,bool mustValidate,string validateGroup,bool isSearch)
        {
            string validate = !col.IsNullable && mustValidate ? IsNotNullable : string.Empty;

            string isNullable = !col.IsNullable ? string.Format(" RequiredFieldValidatorErrMsg='{0} 不能为空！'", col.Description) : string.Empty;

            if (col.DataType == DbType.Date || col.DataType == DbType.DateTime)
            {
                return string.Format(TextBox, GetName(prefix, col), "年月日", validate, validateGroup, string.Empty, isSearch ? string.Empty : "DatePickerJs=\"WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'})\"", isNullable);
            }
            else if (col.DataType == DbType.Int16 || col.DataType == DbType.Int32 || col.DataType == DbType.Int64 || col.DataType== DbType.Byte || col.DataType== DbType.UInt16 || col.DataType== DbType.UInt32||col.DataType== DbType.UInt64)
            {
                return string.Format(TextBox, GetName(prefix, col), "整数", validate, validateGroup, string.Empty, string.Empty, isNullable);
            }
            else if (col.DataType == DbType.Double || col.DataType == DbType.Single || col.DataType== DbType.VarNumeric)
            {
                return string.Format(TextBox, GetName(prefix, col), "浮点数", validate, validateGroup, string.Empty, string.Empty, isNullable);
            }
            else if (col.DataType == DbType.Decimal)
            {
                return string.Format(TextBox, GetName(prefix, col), "金额", validate, validateGroup, string.Empty, string.Empty, isNullable);
            }
            else if (col.DataType == DbType.Boolean)
            {
                return string.Format(CheckBox, GetName(prefix, col),col.Description);
            }
            else if (col.DataType == DbType.Guid)
            {
                return string.Format(DropDownList, GetName(prefix, col), string.Empty, validateGroup);
            }
            else
            {
                return string.Format(TextBox, GetName(prefix, col), "文本", validate, validateGroup, isSearch ? ReplaceSql : string.Empty, (!isSearch && col.MaxLength > 0) ? string.Format(MaxLength, col.MaxLength) : string.Empty, isNullable);
            }
        }
           
    }
}
